package com.jwthhealth_pub.wristband.main.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.BarLineChartBase;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.MPPointF;
import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.utils.Contas;
import com.jwthhealth_pub.utils.TimeUtil;
import com.jwthhealth_pub.wristband.bloodpressure.view.BloodPressureStatisticsActivity;
import com.jwthhealth_pub.wristband.broadcast.WristBandReceiver;
import com.jwthhealth_pub.wristband.main.adapter.MyWarningRecordMsgAdapter;
import com.jwthhealth_pub.wristband.main.model.SleepModule;
import com.jwthhealth_pub.wristband.main.model.WristBandSettingModule;
import com.jwthhealth_pub.wristband.main.widget.SuperCircleView;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.MyAxisValueFormatter;
import com.jwthhealth_pub.wristband.newbloodpressure.utils.XYMarkerView;
import com.jwthhealth_pub.wristband.newbloodpressure.view.NewBloodPressureActivity;
import com.jwthhealth_pub.wristband.rate.view.HeartRateStatisticsActivity;
import com.jwthhealth_pub.wristband.service.WristBandService;
import com.jwthhealth_pub.wristband.service.model.SleepTimeModule;
import com.jwthhealth_pub.wristband.setting.view.WristbandSettingActivity;
import com.jwthhealth_pub.wristband.sleep.view.SleepStatisticsActivity;
import com.jwthhealth_pub.wristband.steps.view.StepsStatisticsActivity;
import com.jwthhealth_pub.wristband.view.SportListActivity;
import com.jwthhealth_pub.wristband.view.StartSportActivity;
import com.jwthhealth_pub.wristband.warning.WarningRecordActivity;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;


/**
 * 手环主页
 * Created by yang on 2017/12/15.
 */

public class WristbandMainActivity extends BaseActivity implements
        OnChartValueSelectedListener {

    private final String TAG = makeLogTag(WristbandMainActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;

    @BindView(R.id.rv_warning_msg)
    RecyclerView rvMsg;
    @BindView(R.id.superview)
    SuperCircleView mSuperCircleView;
    Unbinder unbinder;
    @BindView(R.id.tv)
    TextView tv_steps;

    @BindView(R.id.myWaringCardView)
    CardView myWaringCardView;

    @BindView(R.id.xueyaCardView)
    CardView xueyaCardView;

    @BindView(R.id.sleepStatsCardView)
    CardView sleepStatsCardView;

    @BindView(R.id.ll_step)
    LinearLayout ll_step;
    @BindView(R.id.timingmovementCardView)
    CardView timingmovementCardView;

    @BindView(R.id.barChart)
    BarChart barChart;
    @BindView(R.id.tv_start)
    TextView tv_start;     //显示的比例步数

    @BindView(R.id.tv_nowStep)   //最新的步数
            TextView tv_nowStep;
    @BindView(R.id.tv_calorie)   //卡路里
            TextView tv_calorie;
    @BindView(R.id.tv_km)   //最新的步数
            TextView tv_km;
    @BindView(R.id.tv_days)   //达标的天数
            TextView tv_days;
    @BindView(R.id.tv_heartReate)   //最新的心率
            TextView tv_heartReate;
    @BindView(R.id.tv_oxygen)  //最新的血氧
            TextView tv_oxygen;
    @BindView(R.id.tv_bloodPressure)  //最新的血压
            TextView tv_bloodPressure;
    @BindView(R.id.tv_fatigue)  //疲劳度
            TextView tv_fatigue;
    @BindView(R.id.tv_date)  //日期
            TextView tv_date;
    @BindView(R.id.tv_sleepSum)  //睡眠总时间
            TextView tv_sleepSum;
    @BindView(R.id.tv_deepSleep)  //深睡
            TextView tv_deepSleep;
    @BindView(R.id.tv_lightSleep)  //浅睡
            TextView tv_lightSleep;
    @BindView(R.id.tv_sober)  //清醒
            TextView tv_sober;

    @BindView(R.id.ll_reate)  //心率详情
            LinearLayout ll_reate;
    @BindView(R.id.ll_xueya)  //血压详情
            LinearLayout ll_xueya;
    @BindView(R.id.preview_progressBar)  //进度条
            ProgressBar preview_progressBar;


    int DAYARRY = 7;

    private WristBandService mService;
    private WristBandReceiver mWristBandReceiver;
    boolean isConn = false; //手环是否连接
    boolean isSleppValueNotNULL = false; //是否有睡眠数据
    boolean isReateValueNotNULL = false; //是否有心率数据
    boolean isOxyBloodValueNotNULL = false; //是否有血氧数据

    String xinlvValue, shuzhangyaValue, shousouyaVale, shuzhangya_normal, shousuoya_normal, xinlv_normal;
    private ArrayList<String> sleepValues = new ArrayList<>();
    String  ValueSteps="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_wristband);
        ButterKnife.bind(this);
        initView();
        initBarDatas();
        updateWristBandSetting();

            Log.d(TAG, "here1");
            refreshSleepCard();
    }

    private void refreshSleepCard() {
        SleepModule module = Contas.getObject(this, SleepModule.class);
        if (module == null) return;

        Log.d(TAG, "here2");

        ArrayList<String> sleepValuesT = new ArrayList<>();
        sleepValuesT.add(0, module.startSleepTime);
        sleepValuesT.add(1, module.endSleepTime);
        sleepValuesT.add(2, module.deepSleepTime);
        sleepValuesT.add(3, module.lightSleepTime);
        sleepValuesT.add(4, module.awakTime);
        for (int i = 0; i < 24; i++) {
            sleepValuesT.add(i + 5, module.hoursData.get(i));
        }

        isSleppValueNotNULL = true;
        int totleTitme = Integer.parseInt(sleepValuesT.get(1)) - Integer.parseInt(sleepValuesT.get(0));
        tv_sleepSum.setText(TimeUtil.second2hourmin(totleTitme));
        tv_deepSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(2))));
        tv_lightSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(3))));
        tv_sober.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(4))));
        preview_progressBar.setSecondaryProgress(Integer.parseInt(sleepValuesT.get(2)));
        preview_progressBar.setProgress(Integer.parseInt(sleepValuesT.get(3)));
        preview_progressBar.setMax(Integer.parseInt(sleepValuesT.get(4)));
        // String startSleepTime = sleepTimeModule.getStartSleepTime();   String endSleepTime = sleepTimeModule.getEndSleepTime();

        sleepValues = sleepValuesT;

        Log.d(TAG, "here3");
       String  nowStep = Contas.getName(WristbandMainActivity.this,"tv_nowStep");
        String  calorie =   Contas.getName(WristbandMainActivity.this,"tv_calorie");
        String  km =  Contas.getName(WristbandMainActivity.this,"tv_km");
        if(!nowStep.isEmpty()){
            tv_nowStep.setText(nowStep);
        }
        if(!calorie.isEmpty()){
            tv_calorie.setText(calorie);
        }
        if(!km.isEmpty()){
            tv_km.setText(km);
        }



    }

    private void updateWristBandSetting() {

        LoginModel.DataBean userinfoModel = Contas.getObject(this, LoginModel.DataBean.class);

        if (userinfoModel == null) return;

        showLoadProgresbar();
        Call<WristBandSettingModule> wristBandSettingModuleCall = ApiHelper.updataWristBandSetting(userinfoModel.getId(), userinfoModel.getAndroidtoken());
        wristBandSettingModuleCall.enqueue(new Callback<WristBandSettingModule>() {
            @Override
            public void onResponse(Call<WristBandSettingModule> call, Response<WristBandSettingModule> response) {
                WristBandSettingModule body = response.body();
                if (body == null) return;

                String code = body.getCode();
                if (code != null && code.equals("0")) {
                    Contas.putObject(WristbandMainActivity.this, body.getClass());
                    tv_days.setText(body.getData().getBiaozhun().getTotal_days());
                    //这是达标天数的赋值
                    shuzhangya_normal = body.getData().getBiaozhun().getShuzhangya();
                    shousuoya_normal = body.getData().getBiaozhun().getShousuoya();
                    xinlv_normal = body.getData().getBiaozhun().getXinlv();

                } else {
                    Log.d(TAG, body.getMsg());
                }
                missProgressbar();
            }

            @Override
            public void onFailure(Call<WristBandSettingModule> call, Throwable t) {
                LogUtil.e(t.toString(), TAG);
                missProgressbar();
            }
        });

    }

    private void missProgressbar() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                missLoadProgressbar();
            }
        });
    }

    @OnClick(R.id.ll_step)
    //步数统计
    public void stepsGoStatsClick() {
        Intent intent = new Intent(this, StepsStatisticsActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.xueyaCardView)
    //当前脉搏血压状态
    public void xueyaStatsClick() {
        isConn = mService.isConn;
        if (isConn & isReateValueNotNULL) {
            Intent intent = new Intent(this, NewBloodPressureActivity.class);
            intent.putExtra("xinlvValue", xinlvValue);
            intent.putExtra("shousouyaVale", shousouyaVale);
            intent.putExtra("shuzhangyaValue", shuzhangyaValue);
            intent.putExtra("xinlv_normal", xinlv_normal);
            intent.putExtra("shousuoya_normal", shousuoya_normal);
            intent.putExtra("shuzhangya_normal", shuzhangya_normal);
            startActivity(intent);
        }

    }

    @OnClick(R.id.myWaringCardView)
    //我的预警
    public void myWaringClick() {
        Intent intent = new Intent(this, WarningRecordActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.sleepStatsCardView)
    //当前睡眠状态
    public void sleepStatsClick() {
        isConn = mService.isConn;
        if (isConn & isSleppValueNotNULL) {
            Intent intent = new Intent(this, SleepStatisticsActivity.class);
            intent.putStringArrayListExtra("sleepValues", sleepValues);
            startActivity(intent);

        }

    }

    @OnClick(R.id.timingmovementCardView)
    //计时运动详情
    public void timingmovementCardViewClick() {
        Intent intent = new Intent(this, SportListActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.tv_start)
    //开始运动
    public void startClick() {
        Intent intent = new Intent(this, StartSportActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.ll_reate)
    //心率详情
    public void xinLvClick() {
        Intent intent = new Intent(this, HeartRateStatisticsActivity.class);
        startActivity(intent);
    }

    //血压详情
    @OnClick(R.id.ll_xueya)
    public void xueyaClick() {
        Intent intent = new Intent(this, BloodPressureStatisticsActivity.class);
        startActivity(intent);
    }

    String mubiaoValueSteps = "";

    private void initView() {

//        tv_nowStep.setText("1000步");//设置显示内容
//        tv_nowStep.setDuration(2000);//设置完成跳动的持续时长(单位ms)
//        tv_nowStep.setFormat("%.0f");//设置数字的显示格式
//        tv_nowStep.dance();//启动效果,开始数字跳动

        tv_date.setText(TimeUtil.getPastDate(1));

        mubiaoValueSteps = Contas.getName(this, "mTargetSetp");
        mSuperCircleView.setShowSelect(false);
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
        valueAnimator.setTarget(tv_steps);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int i = Integer.valueOf(String.valueOf(animation.getAnimatedValue()));

                if (!mubiaoValueSteps.isEmpty()&&!ValueSteps.isEmpty()) {
                    int value = Integer.valueOf(ValueSteps);
                    mubiaoValueSteps = mubiaoValueSteps.replace("步", "");
//                    mSuperCircleView.setSelect((int) (300));
//                     创建一个数值格式化对象
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    // 设置精确到小数点后几位
                    numberFormat.setMaximumFractionDigits(0);
                    String result = numberFormat.format((float) value / (float) Integer.valueOf(mubiaoValueSteps) * 100);
                    mSuperCircleView.setSelect((int) (Integer.valueOf(result) * (i / 100f)));
                    tv_steps.setTextColor(getResources().getColor(R.color.whilelow));
                    tv_steps.setText("已完成" + result + "%");
                } else {
                    tv_steps.setText("已完成" + "一" + "%");
                    tv_steps.setTextColor(getResources().getColor(R.color.whilelow));
                    mSuperCircleView.setSelect(0);
                }

            }
        });
        valueAnimator.start();
        rvMsg.setLayoutManager(new LinearLayoutManager(this));
        rvMsg.setAdapter(new MyWarningRecordMsgAdapter(this));

    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {
                Intent intent = new Intent(WristbandMainActivity.this, WristbandSettingActivity.class);
                startActivity(intent);
            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    private void initBarDatas() {
        barChart.setOnChartValueSelectedListener(this);
        barChart.animateY(1000, Easing.EasingOption.Linear);
        barChart.animateX(1000, Easing.EasingOption.Linear);
        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);


        barChart.setHighlightFullBarEnabled(true);
        barChart.getDescription().setEnabled(false);
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        barChart.setMaxVisibleValueCount(300);

        // scaling can now only be done on x- and y-axis separately
        barChart.setPinchZoom(false);

        barChart.setDrawGridBackground(false);//是否显示网格

        IAxisValueFormatter xAxisFormatter = new DayFormatter(barChart);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);
        xAxis.setTextSize(10.5f);


        IAxisValueFormatter custom = new MyAxisValueFormatter();

        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setDrawGridLines(false);//是否画线
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        leftAxis.setEnabled(false);

        YAxis rightAxis = barChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        rightAxis.setLabelCount(8, false);
        rightAxis.setValueFormatter(custom);
        rightAxis.setSpaceTop(15f);
        rightAxis.setDrawGridLines(false);//是否画线
        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        rightAxis.setEnabled(false);

        barChart.getLegend().setEnabled(false);

        XYMarkerView mv = new XYMarkerView(this, xAxisFormatter);
        mv.setChartView(barChart); // For bounds control
        barChart.setMarker(mv); // Set the marker to the chart
        barChart.setBorderWidth(50f);
        setBarChartData(3, 30);
    }

    private void setBarChartData(int p, int temp) {
        float start = 1f;

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 1; i <= DAYARRY; i++) {

            BarEntry barEntry = new BarEntry(i, (int) (1 + Math.random() * 300));
            yVals1.add(barEntry);
        }

        BarDataSet set1;  //这就是每个柱状图的实例

        if (barChart.getData() != null &&
                barChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();


        } else {
            set1 = new BarDataSet(yVals1, "步数");
            set1.setDrawIcons(false);

            int[] PASTEL_COLORS = {  //设置每个柱状图背景色
                    Color.rgb(75, 204, 209)
            };
            set1.setColors(PASTEL_COLORS);
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);//调节顶部字体大小
            data.setValueTextColor(R.color.mainMotion_li);

            data.setBarWidth(0.20f); //调节间距的
            barChart.setData(data);
        }
    }


    protected RectF mOnValueSelectedRectF = new RectF();

    @SuppressLint("NewApi")
    @Override
    public void onValueSelected(Entry e, Highlight h) {

        if (e == null)
            return;

        RectF bounds = mOnValueSelectedRectF;
        barChart.getBarBounds((BarEntry) e, bounds);
        MPPointF position = barChart.getPosition(e, YAxis.AxisDependency.LEFT);

//        Log.i("bounds", bounds.toString());
//        Log.i("position", position.toString());
//
//        Log.i("x-index",
//                "low: " + barChart.getLowestVisibleX() + ", high: "
//                        + barChart.getHighestVisibleX());

        MPPointF.recycleInstance(position);
    }

    @Override
    public void onNothingSelected() {
    }


    //这是设置下端的数值
    class DayFormatter implements IAxisValueFormatter {
        private BarLineChartBase<?> chart;

        public DayFormatter(BarLineChartBase<?> chart) {
            this.chart = chart;
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            String appendix = "";

            if (value == 1.0) {
                appendix = "12/13";
            }
            if (value == 2.0) {
                appendix = "12/14";
            }
            if (value == 3.0) {
                appendix = "12/15";
            }
            if (value == 4.0) {
                appendix = "12/16";
            }
            if (value == 5.0) {
                appendix = "12/17";
            }
            if (value == 6.0) {
                appendix = "12/18";
            }
            if (value == 7.0) {
                appendix = "12/19";
            }
            return appendix;
        }
    }

    private void notifyData() {
        mWristBandReceiver = new WristBandReceiver();
        mWristBandReceiver.setOnWristBandDataChanged(new WristBandReceiver.onWristBandDataChanged() {
            @Override
            public void onStepChange(List<String> params) {
                Log.i("tag", "step:" + params.get(0) + " kcal:" + params.get(1) + " odo:" + params.get(2));
                if (!params.isEmpty()) {
                    ValueSteps = params.get(0);
                    tv_nowStep.setText(params.get(0) + "步");
                    tv_calorie.setText(params.get(1));
                    tv_km.setText(params.get(2));

                    Contas.putName(WristbandMainActivity.this,"tv_nowStep",params.get(0) + "步");
                    Contas.putName(WristbandMainActivity.this,"tv_calorie",params.get(1));
                    Contas.putName(WristbandMainActivity.this,"tv_km",params.get(2) );
                }


            }

            @Override
            public void onHeartChange(String value) {
                Log.i("tag", "heart:" + value);
            }

            @Override
            public void onBloodPressureChange(ArrayList<String> value) {
                Log.i("tag", "收缩压:" + value.get(0) + " 舒张压:" + value.get(1) + " 心率:" + value.get(2));
                Log.d(TAG, "收缩压:" + value.get(0) + " 舒张压:" + value.get(1) + " 心率:" + value.get(2));
                if (!value.isEmpty()) {
                    isReateValueNotNULL = true;
                    tv_bloodPressure.setText(value.get(0) + "/" + value.get(1));
                    tv_heartReate.setText(value.get(2));
                    xinlvValue = value.get(2);
                    shousouyaVale = value.get(0);
                    shuzhangyaValue = value.get(1);
                }

            }

            @Override
            public void onOxyBloodChange(String oxyValue) {
                Log.i("tag", "血氧: " + oxyValue);
                Log.d(TAG, "血氧: " + oxyValue);
                if (!oxyValue.isEmpty()) {
                    isOxyBloodValueNotNULL = true;
                    tv_oxygen.setText(oxyValue);
                }

            }

            @Override
            public void onFatifueChange(String fatifue) {
                Log.i(TAG, "疲劳度: " + fatifue);
                if (!fatifue.isEmpty()) {
                    if (fatifue.equals("0"))
                        tv_fatigue.setText("良好");
                    else if (fatifue.equals("1"))
                        tv_fatigue.setText("正常");
                    else if (fatifue.equals("2"))
                        tv_fatigue.setText("轻度疲劳");
                    else
                        tv_fatigue.setText("注意休息");
                }
            }

            @Override
            public void onSleepChange(ArrayList<String> sleepValuesT) {

                if (!sleepValuesT.isEmpty()) {

                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < 24; i++) {
                        list.add(sleepValuesT.get(i + 5));
                    }


                    SleepTimeModule module = new SleepTimeModule(sleepValuesT.get(0), sleepValuesT.get(1),
                            sleepValuesT.get(4), sleepValuesT.get(3), sleepValuesT.get(2), null);

                    SleepModule bean = new SleepModule();
                    bean.startSleepTime = sleepValuesT.get(0);
                    bean.endSleepTime = sleepValuesT.get(1);
                    bean.awakTime = sleepValuesT.get(2);
                    bean.lightSleepTime = sleepValuesT.get(3);
                    bean.deepSleepTime = sleepValuesT.get(4);
                    bean.hoursData = list;

                    Contas.putObject(WristbandMainActivity.this,bean);
                    SleepModule object = Contas.getObject(WristbandMainActivity.this, SleepModule.class);
                    if (object == null)
                        Log.d(TAG, "object is null");
                    else
                        Log.d(TAG, "object isn't null");

                    isSleppValueNotNULL = true;
                    int totleTitme = Integer.parseInt(sleepValuesT.get(1)) - Integer.parseInt(sleepValuesT.get(0));
                    tv_sleepSum.setText(TimeUtil.second2hourmin(totleTitme));
                    tv_deepSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(2))));
                    tv_lightSleep.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(3))));
                    tv_sober.setText(TimeUtil.minute2hourmin(Integer.parseInt(sleepValuesT.get(4))));
                    preview_progressBar.setSecondaryProgress(Integer.parseInt(sleepValuesT.get(2)));
                    preview_progressBar.setProgress(Integer.parseInt(sleepValuesT.get(3)));
                    preview_progressBar.setMax(Integer.parseInt(sleepValuesT.get(4)));
                    // String startSleepTime = sleepTimeModule.getStartSleepTime();   String endSleepTime = sleepTimeModule.getEndSleepTime();

                    sleepValues = sleepValuesT;

                }
            }
        });

        IntentFilter wristBandReceiver = new IntentFilter();
        wristBandReceiver.addAction(Constant.PACKAGENAME);
        registerReceiver(mWristBandReceiver, wristBandReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        notifyData();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWristBandReceiver != null)
            unregisterReceiver(mWristBandReceiver);
    }


}
